package org.onvivals.app.modules.pms.controller;

import cn.hutool.core.convert.Convert;
import com.alibaba.dashscope.audio.ttsv2.enrollment.Voice;
import com.alibaba.dashscope.audio.ttsv2.enrollment.VoiceEnrollmentService;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.onvivals.common.web.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @className: PmsVoiceController
 * @description: TODO
 * @author: weirdor
 * @date: 2025/1/7
 **/
@Tag(name = "语音合成管理")
@RestController
@RequestMapping("/voice")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class PmsVoiceController {
	public static String apiKey = "sk-e9e8dcae1380490a912bb883a884b41f";  // 如果您没有配置环境变量，请在此处用您的API-KEY进行替换
	private static String prefix = "prefix";
	private static String targetModel = "cosyvoice-v1";

	/**
	 * 上传语音
 	 */
	@Operation(summary = "复刻声音")
	@Parameter(name = "fileUrl", description = "文件路径", required = true)
	@PostMapping("/copy")
	public Result<?> copyVoice(@RequestBody Map<String, Object> map) throws NoApiKeyException, InputRequiredException {
		String fileUrl = Convert.toStr(map.get("fileUrl"));
		VoiceEnrollmentService service = new VoiceEnrollmentService(apiKey);
		Voice myVoice = service.createVoice(targetModel, prefix, fileUrl);
		System.out.println("RequestId: " + service.getLastRequestId());
		System.out.println("your voice id is " + myVoice.getVoiceId());
		return Result.OK("上传成功", myVoice.getVoiceId());
	}

}
